package com.situ.stmall.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stmall.admin.entity.GoodsSort;
import com.situ.stmall.admin.mapper.GoodsSortMapper;
import com.situ.stmall.admin.service.GoodsSortService;

@Service
public class GoodsSortServiceImpl implements GoodsSortService {

	@Autowired
	private GoodsSortMapper goodsSortMapper;
	
	@Override
	public GoodsSort add(GoodsSort goodsSort) {
		goodsSortMapper.insert(goodsSort);
		return goodsSortMapper.selectByPrimaryKey(goodsSort.getId());
	}

	@Override
	public GoodsSort remove(Integer id) {
		GoodsSort goodsSort = goodsSortMapper.selectByPrimaryKey(id);
		if (goodsSort.getStatus() == 0) {
			goodsSort.setStatus(1);
		} else if (goodsSort.getStatus() == 1){
			goodsSort.setStatus(0);
		}
		
		goodsSortMapper.updateByPrimaryKey(goodsSort);
		return goodsSort;
	}

	@Override
	public GoodsSort edit(GoodsSort goodsSort) {
		goodsSortMapper.updateByPrimaryKey(goodsSort);
		return goodsSortMapper.selectByPrimaryKey(goodsSort.getId());
	}

	@Override
	public PageInfo getByPage(Integer page, Integer limit, GoodsSort goodsSort) {
		if (page == null) {
			page = 1;
		}
		
		if (limit == null) {
			List list =goodsSortMapper.select(goodsSort);
			limit = list.size();
		}
		
		PageHelper.startPage(page, limit);
		List list = goodsSortMapper.select(goodsSort);
		PageInfo pageInfo = new PageInfo<>(list);
		
		return pageInfo;
	}

	

}
